package com.silverhaze.diamond.common.entity;

import com.silverhaze.commons.db.datasource.ObjectEntity;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * © Ivan Babanin (06.07.2007 19:58:10)
 * Project Galaxy. This part of code is strictly confidential.
 */

@Entity()
@Table(name = "users")
public class SystemUser extends ObjectEntity {
    private static final long serialVersionUID = 2689127L;

    @Basic()
    @Column(name = "admin")
    private boolean admin;

    @Basic()
    @Column(name = "login", length = 100)
    private String login;

    @Basic()
    @Column(name = "password", length = 100)
    private String password;

    @Basic()
    @Column(name = "lastlogin")
    private Date lastLogin;

    @OneToMany(targetEntity = Service.class)
    @Column(name = "services")
    private Set<Service> services;

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SystemUser user = (SystemUser) o;

        if (admin != user.admin) return false;
        if (login != null ? !login.equals(user.login) : user.login != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (admin ? 1 : 0);
        result = 31 * result + (login != null ? login.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (lastLogin != null ? lastLogin.hashCode() : 0);
        result = 31 * result + (services != null ? services.hashCode() : 0);
        return result;
    }

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public Set<Service> getServices() {
        return services;
    }

    public void setServices(Set<Service> services) {
        this.services = services;
    }

    public String toString() {
        return getLogin();
    }
}

